package com.kosam.commons.base.entity;

import org.hibernate.annotations.CreationTimestamp;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * Created by tgx on 2017/2/20.
 */
@MappedSuperclass
@EntityListeners({AuditingEntityListener.class})
public abstract class BaseEntity<ID extends Serializable> extends Id<ID>{
    private static final long serialVersionUID = -4829990623102036715L;
    public static final String ID = "id";
    public static final String CREATE_BY = "createBy";
    public static final String CREATE_DATE = "createDate";
    public static final String LAST_MODIFIED_BY = "lastModifiedBy";
    public static final String LAST_MODIFIED_DATE = "lastModifiedDate";
    public static final String VERSION = "version";

    @CreatedBy
    @Column(updatable = false, name = "create_by", nullable = true)
    private String createBy;

    @CreationTimestamp
    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    @Column( updatable = false, name = "create_date", nullable = true )
    private Date createDate;

    @LastModifiedBy
    @Column( name = "last_modified_by", nullable = true )
    private String lastModifiedBy;

    @CreationTimestamp
    @LastModifiedDate
    @Column( name = "last_modified_date", nullable = true )
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastModifiedDate;

    @Version
    @Column( name = "version", nullable = false )
    private Long version;

    public BaseEntity() {

    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public Date getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(Date lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    public boolean isNew() {
        return null == this.getId() || "".equals(this.getId().toString());
    }

    public String toString() {
        return "BaseEntity [id=" + this.id + ", createBy=" + this.createBy + ", createDate=" + this.createDate + ", lastModifiedBy=" + this.lastModifiedBy + ", lastModifiedDate=" + this.lastModifiedDate + ", version=" + this.version + "]";
    }

    public int hashCode() {
        boolean prime = true;
        int result = super.hashCode();
        result = 31 * result + (this.id == null?0:this.id.hashCode());
        return result;
    }

    public boolean equals(Object obj) {
        if(this == obj) {
            return true;
        } else if(!super.equals(obj)) {
            return false;
        } else if(this.getClass() != obj.getClass()) {
            return false;
        } else {
            BaseEntity other = (BaseEntity)obj;
            if(this.id == null) {
                if(other.id != null) {
                    return false;
                }
            } else if(!this.id.equals(other.id)) {
                return false;
            }

            return true;
        }
    }
}
